﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SanQiOther.Models;
namespace SanQiOther.Controllers
{
    public class TustController : Controller  
    {
        private RewardDbContext db = new RewardDbContext();
        // GET: Tust
        public ActionResult Index()
        {
            List<SanQiOther.Models.dict_dept> dept = db.dict_dept.Where(r => r.isUsing == true).ToList<SanQiOther.Models.dict_dept>();
            SanQiOther.Models.dict_dept s = new dict_dept();
            s.id = 0;
            s.mc = "全部";
            dept.Add(s);
            ViewData["DeptList"] = new SelectList(dept, "Id", "mc", 0);

            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "2017", Value = "2017" });
            items.Add(new SelectListItem { Text = "2018", Value = "2018" });
            this.ViewData["YearList"] = items;

            List<SelectListItem> itemss = new List<SelectListItem>();
            itemss.Add(new SelectListItem { Text = "1", Value = "1" });
            itemss.Add(new SelectListItem { Text = "2", Value = "2" });
            itemss.Add(new SelectListItem { Text = "3", Value = "3" });
            itemss.Add(new SelectListItem { Text = "4", Value = "4" });
            itemss.Add(new SelectListItem { Text = "5", Value = "5" });
            itemss.Add(new SelectListItem { Text = "6", Value = "6" });
            itemss.Add(new SelectListItem { Text = "7", Value = "7" });
            itemss.Add(new SelectListItem { Text = "8", Value = "8" });
            itemss.Add(new SelectListItem { Text = "9", Value = "9" });
            itemss.Add(new SelectListItem { Text = "10", Value = "10" });
            itemss.Add(new SelectListItem { Text = "11", Value = "11" });
            itemss.Add(new SelectListItem { Text = "12", Value = "12" });
            this.ViewData["MonthList"] = itemss;
            ViewBag.Dept = 0;
            ViewBag.Year = 0;
            ViewBag.Month = 0;
            return View();
        }


        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            int deptid = Convert.ToInt16(form["DeptList"]);
            int year = Convert.ToInt16(form["YearList"]);
            int month = Convert.ToInt16(form["MonthList"]);

            List<SanQiOther.Models.dict_dept> dept = db.dict_dept.Where(r => r.isUsing == true).ToList<SanQiOther.Models.dict_dept>();
            SanQiOther.Models.dict_dept s = new dict_dept();
            s.id = 0;
            s.mc = "全部";
            dept.Add(s);
            ViewData["DeptList"] = new SelectList(dept, "Id", "mc", 0);

            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "2017", Value = "2017" });
            items.Add(new SelectListItem { Text = "2018", Value = "2018" });
            this.ViewData["YearList"] = items;

            List<SelectListItem> itemss = new List<SelectListItem>();
            itemss.Add(new SelectListItem { Text = "1", Value = "1" });
            itemss.Add(new SelectListItem { Text = "2", Value = "2" });
            itemss.Add(new SelectListItem { Text = "3", Value = "3" });
            itemss.Add(new SelectListItem { Text = "4", Value = "4" });
            itemss.Add(new SelectListItem { Text = "5", Value = "5" });
            itemss.Add(new SelectListItem { Text = "6", Value = "6" });
            itemss.Add(new SelectListItem { Text = "7", Value = "7" });
            itemss.Add(new SelectListItem { Text = "8", Value = "8" });
            itemss.Add(new SelectListItem { Text = "9", Value = "9" });
            itemss.Add(new SelectListItem { Text = "10", Value = "10" });
            itemss.Add(new SelectListItem { Text = "11", Value = "11" });
            itemss.Add(new SelectListItem { Text = "12", Value = "12" });
            this.ViewData["MonthList"] = itemss;

            ViewBag.Dept = deptid;
            ViewBag.Year = year;
            ViewBag.Month = month;

            return View();
        }



        public ActionResult Index_1(int dept, int Year, int Month)
        {
            //10月统计 上月末人数
            //select* from[member] where dept = 7 and isls = 0 and islz = 0 and rzsj<='2017-09-25'
            //union
            //select* from[member] where dept = 7 and isls = 0 and islz = 1 and lzsj> '2017-09-25' and rzsj<='2017-09-25'

            if (dept == 0)
            {
                var member = db.v_member.Where(r => r.dept == dept);
                return View(member.ToList());
            }
            else
            {
                string mid_date = Year.ToString() + "-" + Month.ToString() + "-26";
                DateTime N = Convert.ToDateTime(mid_date);
                DateTime S = N.AddMonths(-1).AddDays(-1);

                var member1 = db.v_member.Where(r => r.dept == dept && r.isls == false && r.islz == false && r.rzsj <= S);
                var member2 = db.v_member.Where(r => r.dept == dept && r.isls == false && r.islz == true && r.lzsj > S && r.rzsj <= S);
                var list = member1.Union(member2).ToList().OrderByDescending(r => r.islz);
                return View(list);
            }
        }
        public ActionResult Index_2(int dept, int Year, int Month)
        {
            //10月统计 本月末人数
            //select* from[member] where dept = 7 and isls = 0 and islz = 0and rzsj<='2017-10-25'
            //union
            //select* from[member] where dept = 7 and isls = 0 and islz = 1 and lzsj> '2017-10-25'

            if (dept == 0)
            {
                var member = db.v_member.Where(r => r.dept == dept);
                return View(member.ToList());
            }
            else
            {
                string mid_date = Year.ToString() + "-" + Month.ToString() + "-25";
                DateTime N = Convert.ToDateTime(mid_date);

                var member1 = db.v_member.Where(r => r.dept == dept && r.isls == false && r.islz == false && r.rzsj <= N);
                var member2 = db.v_member.Where(r => r.dept == dept && r.isls == false && r.islz == true && r.lzsj > N);
                var list = member1.Union(member2).ToList().OrderByDescending(r => r.islz);
                return View(list);
            }
        }
        public ActionResult Index_3(int dept, int Year, int Month)
        {
            //10月统计 本月入职人
            //select* from[member] where dept = 7 and isls = 0 and islz = 0 and rzsj> '2017-09-25' and rzsj< '2017-10-26'
            //union
            //select* from[member] where dept = 7 and isls = 0 and islz = 1 and rzsj> '2017-09-25' and rzsj< '2017-10-26'
            if (dept == 0)
            {
                var member = db.v_member.Where(r => r.dept == dept);
                return View(member.ToList());
            }
            else
            {
                string mid_date = Year.ToString() + "-" + Month.ToString() + "-26";
                DateTime N = Convert.ToDateTime(mid_date);
                DateTime S = N.AddMonths(-1).AddDays(-1);

                var member1 = db.v_member.Where(r => r.dept == dept && r.isls == false && r.rzsj > S && r.rzsj < N).OrderByDescending(r => r.islz);
                return View(member1.ToList());
            }
        }
        public ActionResult Index_4(int dept, int Year, int Month)
        {
            //10月统计 本月离职人
            //select* from[member] where dept = 7 and isls = 0 and islz = 1 and lzsj> '2017-09-25' and lzsj< '2017-10-26'
            if (dept == 0)
            {
                var member = db.v_member.Where(r => r.dept == dept);
                return View(member.ToList());
            }
            else
            {
                string mid_date = Year.ToString() + "-" + Month.ToString() + "-26";
                DateTime N = Convert.ToDateTime(mid_date);
                DateTime S = N.AddMonths(-1).AddDays(-1);

                var member1 = db.v_member.Where(r => r.dept == dept && r.isls == false && r.islz == true && r.lzsj > S && r.lzsj < N);
                return View(member1.ToList());
            }
        }


    }
}